<template>
  <div class="account-box">
    <!--
      1. :validation-schema 绑定rules校验规则
      2. v-slot="{ errors }"
         errors可以获取校验失败错误提示信息:
         1. 如果没有错误，errors为空对象
         2. 如果有错误，errors对象里会包含当前错误表单项的错误提示消息

     -->
    <Form ref="fm" :validation-schema="rules" v-slot="{ errors }" class="form">
      <!-- <p>{{ errors }}</p> -->
      <div class="form-item">
        <div class="input">
          <i class="iconfont icon-user"></i>
          <!-- 1. 用户名
          name 指定校验字段名
           -->
          <Field
            type="text"
            name="account"
            v-model="formData.account"
            :class="{ error: errors.account }"
            placeholder="请输入用户名或手机号"
          />
        </div>
        <!-- 表单验证错误信息提示=>errors.account如果存在，说明有错误！ -->
        <div class="error errtips" v-if="errors.account">
          <i class="iconfont icon-warning" />{{ errors.account }}
        </div>
      </div>
      <div class="form-item">
        <div class="input">
          <i class="iconfont icon-lock"></i>
          <!-- 2. 密码 -->
          <Field
            type="password"
            name="password"
            v-model="formData.password"
            :class="{ error: errors.password }"
            placeholder="请输入密码"
          />
        </div>
        <div
          class="error errtips"
          :key="errors.password"
          v-if="errors.password"
        >
          <i class="iconfont icon-warning" />{{ errors.password }}
        </div>
      </div>
      <div class="form-item">
        <div class="agree">
          <!--
            对于一些非input元素，通过as属性渲染校验的元素
           -->
          <Field as="XtxCheckbox" name="isAgree" v-model="formData.isAgree">
            <span>我已同意</span>
          </Field>
          <!-- <XtxCheckbox /> -->
          <!-- <span>我已同意</span> -->
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <!-- 错误提示消息 -->
        <div class="error errtips" v-if="errors.isAgree">
          <i class="iconfont icon-warning" />{{ errors.isAgree }}
        </div>
      </div>
      <a @click="login" href="javascript:;" class="btn">登录</a>
    </Form>
    <div class="action">
      <img
        src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png"
        alt=""
      />
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>
<script>
import { Form, Field } from 'vee-validate'
import { ref } from 'vue'
// 导入校验规则
import MyRules from '@/utils/rules'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
// 导入消息提示方法
import { msg } from 'rabbit-ui-core'
export default {
  components: {
    Form, // 表单组件
    Field // 表单项组件=》一般替换input
  },
  setup () {
    // 1. 表单数据=》需要定义为响应数据
    const formData = ref({
      account: 'xiaotuxian001', // 账号
      password: '123456', // 密码
      isAgree: false // 是否同意网站用户协议
    })
    // 2. 表单校验规则=> 不需要定义成响应数据
    const rules = {
      account: MyRules.account,
      password: MyRules.password,
      isAgree: MyRules.isAgree
    }

    // 3. 点击登录
    const fm = ref()
    const store = useStore()
    // router === this.$router
    const router = useRouter()
    // route === this.$route
    const route = useRoute()
    console.log('route对象：', route)
    const login = async () => {
      try {
        // fm.value.validate() 返回一步的Promise对象
        const res = await fm.value.validate()
        // console.log('校验通过：', res)
        if (res.valid) {
          // 说明：res.valid=》 true 兜底校验通过 | false 兜底校验失败
          // console.log('进行登录！')
          await store.dispatch('user/getProfileAction', formData.value)

          // == 合并购物车和获取登录人名下的购物车数据 ==
          store.dispatch('cart/mergeCartAction')

          msg({ text: '登录成功！', type: 'success' })
          // 如果存在route.query.redirectUrl 地址，就跳转，相反跳转首页
          router.replace(route.query.redirectUrl || '/')
        }
      } catch (error) {
        console.log('error:', error)
        // 账号或密码错误，获取后台错误提示消息并给用户提示
        msg({ text: error.response.data.message })
      }
    }

    return { formData, rules, login, fm }
  }
}
</script>
<style lang="less" scoped>
// 错误提示=》文字跳动动画=》
// 作业
// 1. 怎么让动画可以随着校验，连续抖动？？
// 2. 退出登录
.errtips {
  color: #e40438;
  animation-name: errtips;
  animation-duration: 0.5s;
}
@keyframes errtips {
  10% {
    transform: translateY(1px);
  }
  20% {
    transform: translateY(-1px);
  }
  30% {
    transform: translateY(1px);
  }
  40% {
    transform: translateY(-1px);
  }
  50% {
    transform: translateY(1px);
  }
  60% {
    transform: translateY(-1px);
  }
  70% {
    transform: translateY(1px);
  }
  80% {
    transform: translateY(-1px);
  }
  90% {
    transform: translateY(1px);
  }
  100% {
    transform: translateY(0);
  }
}
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 20px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
